package com.titanrise.crm.handler;

import cn.hutool.core.util.ObjectUtil;
import com.titanrise.crm.common.enums.LogEffect;
import com.titanrise.crm.common.enums.TradeStatusEnum;
import com.titanrise.crm.common.utils.*;
import com.titanrise.crm.dao.CustomerDao;
import com.titanrise.crm.domain.model.Messages;
import com.titanrise.crm.domain.model.Product;
import com.titanrise.crm.domain.model.Role;
import com.titanrise.crm.domain.model.Trade;
import com.titanrise.crm.domain.model.enums.MessageStatus;
import com.titanrise.crm.domain.vo.UserVo;
import com.titanrise.crm.environment.annotation.Autowired;
import com.titanrise.crm.environment.ioc.SingletonPoolUtil;
import com.titanrise.crm.mapper.UserMapper;

import javax.xml.bind.SchemaOutputResolver;
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.Optional;
import java.util.function.Function;

/**
 * @Description: customerService打印部分
 * @Author: pront
 * @Time:2024-11-25 21:19
 */
public class CustomerServicePrinter {

    @Autowired
    private CustomerDao customerDao;


    private final Function<String, Either<String, String>> nonChecker = Functions.flow(
            ValidationUtil::trimThenRight
    );

    public static void printAllProducts(List<Product> products) {
        System.out.println();
        System.out.println("  |   商品ID   |  商品标题   |  商品价格  | ");
        products.forEach(product -> {
            System.out.println("  |  " + product.getId() + "  |  " + product.getSkuTitle() + "  |  "  + product.getSkuPrice() +  "  |  ");
        });
    }

    public static void printUserInfo(UserVo userInfo) {
        Console.println("姓名: " + getDefaultValue(userInfo.getName()), LogEffect.GREEN, LogEffect.BOLD);
        Console.println("密码: " + getDefaultValue(userInfo.getPassword()), LogEffect.GREEN, LogEffect.BOLD);
        Console.println("账号名: " + getDefaultValue(userInfo.getUsername()), LogEffect.GREEN, LogEffect.BOLD);
        Console.println("用户描述: " + getDefaultValue(userInfo.getDescription()), LogEffect.GREEN, LogEffect.BOLD);
        Console.println("年收入: " + getDefaultValue(userInfo.getAnnualIncome()), LogEffect.GREEN, LogEffect.BOLD);
        Console.println("从事行业: " + getDefaultValue(userInfo.getIndustry()), LogEffect.GREEN, LogEffect.BOLD);
        Console.println("国家: " + getDefaultValue(userInfo.getCountry()), LogEffect.GREEN, LogEffect.BOLD);
        Console.println("省: " + getDefaultValue(userInfo.getProvince()), LogEffect.GREEN, LogEffect.BOLD);
        Console.println("市: " + getDefaultValue(userInfo.getCity()), LogEffect.GREEN, LogEffect.BOLD);
        Console.println("区: " + getDefaultValue(userInfo.getDistrict()), LogEffect.GREEN, LogEffect.BOLD);
        Console.println("邮箱地址: " + getDefaultValue(userInfo.getEmail()), LogEffect.GREEN, LogEffect.BOLD);
    }

    private static <T> String getDefaultValue(T t) {
        return ObjectUtil.isNotNull(t) ? t.toString() : "空(NULL)";
    }

    public static void printRelativeSalesperson(String salesId) {
        Optional.ofNullable(SingletonPoolUtil.getBean(CustomerDao.class).selectRoleByUid(salesId)).ifPresent(role -> {
            System.out.println("负责的销售姓名: " + role.getName());
        });
    }

    public static void printAllMsg(List<Messages> msgList) {
        int count = 1;
        for (Messages messages : msgList) {
            LogEffect st = LogEffect.GREEN;
            if (count == 1) System.out.println("  |  消息编号  |  消息内容  |  状态 |  ");
            if (messages.getStatus().equals(MessageStatus.READ)) st = LogEffect.RED;
            Console.println("" + (count++) + "  |  " + messages.getContent().substring(0, 2) + "......", st, LogEffect.BOLD);
        }
    }

    public static void printTradeInfo(Trade trade) {
        if (trade != null) {
            Console.println("交易商品: " + trade.getGoodsName(), LogEffect.GREEN, LogEffect.BOLD);
            Console.println("交易数量: " + trade.getNumber(), LogEffect.GREEN, LogEffect.BOLD);
            Console.println("合计金额: " + trade.getPrice(), LogEffect.GREEN, LogEffect.BOLD);
            for (TradeStatusEnum tradeStatusEnum : TradeStatusEnum.values()) {
                if (tradeStatusEnum.getStatus().equals(trade.getStatus())) {
                    Console.println("交易状态: " + tradeStatusEnum.getMsg(), LogEffect.GREEN, LogEffect.BOLD);
                    break;
                }
            }
            Console.println("负责处理的销售: " + getSalespersonName(trade.getSalespersonId()), LogEffect.GREEN, LogEffect.BOLD);
        }
    }

    @Deprecated
    private static String getSalespersonName(String salesId) {
        return SingletonPoolUtil.getBean(CustomerDao.class).selectRoleByUid(salesId).getName();
    }

    @Deprecated
    private static String getClientName(String clientId) {
        return SingletonPoolUtil.getBean(CustomerDao.class).selectRoleByUid(clientId).getName();
    }

    public static void printContract(Trade trade) {
        Console.println("合同id: " + trade.getTradeId(), LogEffect.GREEN, LogEffect.BOLD);
        Console.println("交付产品: " + trade.getGoodsName(), LogEffect.GREEN, LogEffect.BOLD);
        Console.println("支付数量: " + trade.getNumber(), LogEffect.GREEN, LogEffect.BOLD);
        for (TradeStatusEnum tradeStatusEnum : TradeStatusEnum.values()) {
            if (tradeStatusEnum.getStatus().equals(trade.getStatus())) {
                Console.println("交易状态: " + tradeStatusEnum.getMsg(), LogEffect.GREEN, LogEffect.BOLD);
                break;
            }
        }
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        Role client = SingletonPoolUtil.getBean(CustomerDao.class).selectRoleByUid(trade.getClientId());
        Role salesperson = SingletonPoolUtil.getBean(CustomerDao.class).selectRoleByUid(trade.getSalespersonId());
        String clientPhoneNumber = Optional.ofNullable(SingletonPoolUtil.getBean(UserMapper.class).selectById(client.getUid()).getPhoneNumber()).orElse("");
        String salesPhoneNumber = Optional.ofNullable(SingletonPoolUtil.getBean(UserMapper.class).selectById(salesperson.getUid()).getPhoneNumber()).orElse("");
        Console.println("合计金额: " + trade.getPrice(), LogEffect.GREEN, LogEffect.BOLD);
        Console.println("负责方姓名: " + getSalespersonName(trade.getSalespersonId()) + "  " + "负责方联系方式: " + clientPhoneNumber, LogEffect.GREEN, LogEffect.BOLD);
        Console.println("支付方姓名: " + getClientName(trade.getClientId()) + "  " + "支付方联系方式: " + salesPhoneNumber, LogEffect.GREEN, LogEffect.BOLD);
        Console.println("成交日期: " + trade.getFinishDate().format(formatter), LogEffect.GREEN, LogEffect.BOLD);
        Console.println("成立平台: " + "CRM(客户关系管理系统)", LogEffect.GREEN, LogEffect.BOLD);
    }

    public static void printTradeStatus(List<Trade> tradeList) {
        System.out.println("|  交付产品  |  交付备注  |  交付状态  |");
        for (Trade trade : tradeList) {
            if (TradeStatusEnum.PAID.getStatus().equals(trade.getStatus())) {
                Console.println("|  " + trade.getGoodsName() + "  |  " + Optional.ofNullable(trade.getNote()).orElse("") + "  |  " + TradeStatusEnum.PAID.getMsg() +  "  |  ", LogEffect.YELLOW, LogEffect.BOLD);
            } else if (TradeStatusEnum.CONFIRM_DEAL.getStatus().equals(trade.getStatus())) {
                Console.println("|  " + trade.getGoodsName() + "  |  " + Optional.ofNullable(trade.getNote()).orElse("") + "  |  " + TradeStatusEnum.CONFIRM_DEAL.getMsg() +  "  |  ", LogEffect.GREEN, LogEffect.BOLD);
            }
        }
    }

    public static void printByIdProduct(Product product) {
        System.out.println("  |  商品标题  |  商品介绍  |  ");
        System.out.println("  |  " + product.getSkuTitle() + "  |  " + product.getSkuDesc() + "  |  " );

    }
}
